import {
  ControlController,
  ISearchBarState,
  ISearchBarEvent,
  ISearchBarController,
} from '@ibiz-template/runtime';
import { ISearchBar } from '@ibiz/model-core';

/**
 * 搜索栏控制器
 *
 * @author chitanda
 * @date 2022-07-24 15:07:07
 * @export
 * @class SearchBarController
 * @extends {ControlController}
 */
export class SearchBarController
  extends ControlController<ISearchBar, ISearchBarState, ISearchBarEvent>
  implements ISearchBarController
{
  /**
   * 占位符
   * @return {*}
   * @author: zhujiamin
   * @Date: 2023-08-11 14:13:10
   */
  placeHolder = '';

  protected initState(): void {
    super.initState();
    this.state.query = '';
  }

  protected async onCreated(): Promise<void> {
    await super.onCreated();
    const appDataEntity = await ibiz.hub.getAppDataEntity(
      this.model.appDataEntityId!,
      this.context.srfappid,
    )!;
    if (appDataEntity) {
      const searchField = appDataEntity.appDEFields?.find(field => {
        return field.enableQuickSearch;
      });
      if (searchField?.lnlanguageRes && searchField.lnlanguageRes.lanResTag) {
        this.placeHolder = ibiz.i18n.t(
          searchField.lnlanguageRes.lanResTag,
          searchField.lnlanguageRes.defaultContent,
        );
      } else if (searchField?.logicName) {
        this.placeHolder = searchField.logicName;
      }
    }
  }

  /**
   * 处理输入
   * @param {string} val
   * @return {*}
   * @author: zhujiamin
   * @Date: 2023-06-01 18:08:07
   */
  handleInput(val: string): void {
    this.state.query = val;
  }

  /**
   * 处理搜索
   * @return {*}
   * @author: zhujiamin
   * @Date: 2023-06-01 18:08:18
   */
  handleSearch(): void {
    this.evt.emit('onSearch', undefined);
  }
}
